// 引入Vue实例
import Vue from 'vue'
// 引入Vuex状态管理
import store from '@/store/index.js';
import refer from '@/utils/refer.js'
import service from '@/service.js'
import qqmapsdk from '@/utils/location.js';
// 定义一个handle类
class handle {
	constructor(args) {
		return args;
	}
	/*** 授权登陆 
	*@param {object} ext  登陆所需扩展字段  
	* 微信小程序  nickName，encryptedData，iv，promotionId 
	* 支付宝   nickName，
	*@param  {string} toUrl 登陆成功跳转地址,引用refer。js实现 
	* auth(,{provider:'weixin',nickName:'aaa'})
	***/
	auth(ext){
		// 这里获取接口地址
		
	//	this.getJingWeiDu();// 获取详细地址信息
		// end
		try{
			const _this = this
			uni.login({
			  provider:ext.provider,
			  //#ifdef  MP-ALIPAY
				scopes:'auth_base',
			  // #endif
			  success: function (loginRes) {
				  console.log('8')
				// console.log(loginRes);
				// let code_channel=uni.getStorageSync('code_channel')?uni.getStorageSync('code_channel'):'oxw';
					// console.log(uni.getStorageSync('code_channel'))
				//loginRes {errMsg: "login:ok", code: "001o84X61hHtTM1AihX61QBiX61o84XD"}
				//uni.setStorageSync('token','');
				//console.log(_this.encryptedData)
				//console.log(_this.iv)
				//#ifdef  MP-WEIXIN 
					let data={
						code:loginRes.code,
						nick_name:ext.nickName,
						provider:'weixin',
						encrypted_data:ext.encryptedData,
						iv:ext.iv,
						province:uni.getStorageSync('loginProvince'),
						city:uni.getStorageSync('loginCity'),
						area:uni.getStorageSync('loginArea'),
						adv_channel: uni.getStorageSync('adv_channel'),
						code_channel:uni.getStorageSync('code_channel'),
						adv_id:uni.getStorageSync('advId'),
						inviteUserId:uni.getStorageSync('inviteUserId'),
						oldOpenid:uni.getStorageSync('oldOpenid')
					}
				// #endif	
				//#ifdef  MP-ALIPAY
					let data={
						authCode:loginRes.authCode,
						provider:'alipay',
						nick_name:ext.nickName
					
					}
				// #endif
				
					var share_status = store.getters.share_status
					
					if(share_status){
						data.share_status = true
						data.share_user_id = store.getters.share_user_id
					}
					
					if(store.getters.promotionId>0)
					{
						data.promotion_id = store.getters.promotionId
					}
					// console.log(data)
				var opt={
					url:'/login/login',
					data:data,
					success:function(res){
						// console.log(res)
						if(res.status==1){							 
							store.dispatch('setToken',res.data.token)
							//console.log(res.data.token)
							store.dispatch('changeLogin',true)
							// _this.getCityServer();//app.vue
								uni.showToast({
									icon:'none',
									title:'登录成功',
									duration: 1000,
									success:function(){
										//console.log('为什么不弹窗,就跳转了')
										//console.log(uni.getStorageSync('share_status'))
										if(store.getters.share_status){
											//进入分享页面
											
												var share_product_id = store.getters.share_product_id
												//清除缓存	share_user_id	share_status	share_product_id
												store.dispatch('setShareUserid','')
												store.dispatch('setShareStatus','')
												store.dispatch('setShareProductid','')
												refer.setVilit('/pages/product/detail/detail')
												let callback = function(url){
													
													
												}						
												refer.toUrl(callback,{productid:share_product_id},'reLaunch')
											
											return
										}
										
										let callback = function(url){
											
											
										}
										let str='/packageAfterSale/pages/after_sales/product/scian-session/scian-list'
										// 暂时使用的方法
										
										if(str==refer.getVilit()){
											refer.toUrl(callback,{},'close')
										}else if(refer.getVilit()== ''){
											// 强制执行当前页面 重新加载onLoad(); 页面不跳转
											// 页面不跳转登录 统一设置 setVilit = ''  
											let pages = getCurrentPages();
											let currPage = pages[pages.length-1]
											currPage.onLoad(currPage.options)//options 参数
											// currPage.onShow();
											// var param1 = JSON.parse(uni.getStorageSync('param'))
											// refer.toUrl(callback,param1,'reLaunch')
										}
										else{
											try{
												var param = JSON.parse(uni.getStorageSync('param'))
											}catch(e){
												var param = {}
											}finally{
												refer.toUrl(callback,param)
											}
													
															
															
											
										}
										
									
										
									}
								});
									
						}else{
							uni.showToast({
								icon:'none',
								title:res.message
							});
						}
					}
				};
				service.GetApi(opt);
				
			  }
			});
			return true
		}catch(exception){
			return false
		}
		
	}

	
	getCityServer(){
		const _this = this
		//_this.$refs['product'].open()
		//获取用户信息
		var opt = {
			url: '/CityServerSite/getCityServer',
			data: {
				province:uni.getStorageSync('loginProvince'),
				city:uni.getStorageSync('loginCity'), 
				lat:uni.getStorageSync('loginlatitude'),
				lng:uni.getStorageSync('loginlongitude')
			},
			success: function(res) {
				if (res.status == 1) {
					
				}
			}
		};
		service.GetApi(opt)
	}
	//app微信授权  start
	appAuthWeixin(){
		uni.login({
		  provider: 'weixin',
		  success: function (res) {
			let desString = JSON.stringify(res.authResult) 
			//console.log(desString)
			//加密
			desString = service.encryptDes(desString)
			//console.log(desString)
			uni.getUserInfo({
			      provider: 'weixin',
			      success: function (infoRes) {
			      // console.log('用户昵称为：' + infoRes.userInfo.nickName);
				   //console.log(99);
				  //console.log(infoRes)
					let nickName =  infoRes.userInfo.nickName
					let data={
						des_string:desString,
						provider:'weixin',
						nick_name:nickName,
						avatar_url:infoRes.userInfo.avatarUrl
					}
					var opt={
						url:'/app/login',
						data:data,
						success:function(res){
							//console.log(777)
							//console.log(res)
							if(res.status==1){							 
								store.dispatch('setToken',res.data.token)
								//console.log(res.data.token)
								store.dispatch('changeLogin',true)
									uni.showToast({
										icon:'none',
										title:'登录成功',
										duration: 3000,
										success:function(){
											//console.log('为什么不弹窗,就跳转了')
											//console.log(uni.getStorageSync('share_status'))
											if(store.getters.share_status){
												//进入分享页面
												
													var share_product_id = store.getters.share_product_id
													//清除缓存	share_user_id	share_status	share_product_id
													store.dispatch('setShareUserid','')
													store.dispatch('setShareStatus','')
													store.dispatch('setShareProductid','')
													refer.setVilit('/pages/product/detail/detail')
													let callback = function(url){
														
													}							
													refer.toUrl(callback,{productid:share_product_id},'reLaunch')  
												
												return
											}
											
											let callback = function(url){
											
											}
											refer.toUrl(callback,{})  
										
											
										}
									});
										
							}else{
								uni.showToast({
									icon:'none',
									title:res.message
								});
							}
						}
					};
					service.GetApi(opt);
			      }
			});
		  
		  }
		});
	
	}
	//app微信授权   end	
	getQueryVariable(variable,url)
	{
			 
		//var url = 'https://api.aiouning.com/wechat/product/param/?sid=12&pid=37&bid=1&cid=2';
		var vars = url.split("param/?");
	   // vars[1] = vars[1].replace(/%3D/g, "=").replace(/%26/g, "&")
		//console.log(vars)
		var param = vars[1].split("&");
	   
		for (var i = 0; i < param.length;i++) {
		  var pair = param[i].split("=");
		  //console.log(pair)
		  if (pair[0] == variable) { return pair[1]; }
		  }
		  return (false);
	}

	/* 获取登陆状态 */
	getLoginStatus() {
		return store.getters.loginStatus
		//console.log(store.loginStatus)
	}
	
	
	
	
}
export default new handle();
